const mta = require('../../../mta_sdk/mta_analysis.js');
var tool = require('../../../utils/util.js');
var ctx = null;
const app = getApp();
var school = '';
var classs = '';
var name = '';
var pid = '';
var pic = '';
var bgpic = '';
var shareImg = ''
Page({

  data: {
    shareImg: '',
    shared: 0,
    ajaxSuccess: false
  },

  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt);
    if (opt.shared == 1) {
      this.setData({
        shared: 1,
        shareImg: opt.shareImg || '',
        ajaxSuccess: true
      });

    }
    if (opt.name) {

      this.makeImg(opt.school, opt.class, opt.name, opt.pic, opt.bgpic)

    }
    school = opt.school;
    classs = opt.class;
    name = opt.name;
    pid = opt.pid;
    pic = opt.pic;
    bgpic = opt.bgpic
  },

  makeImg(school, classs, name, pic, bgpic) {
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    });


    var sysInfo = wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .038;
    var query = wx.createSelectorQuery();
    ctx = wx.createCanvasContext("mycanvas");


    query.select('#canvas').boundingClientRect();//获取canvas的元素信息
    query.select('#headstyle').boundingClientRect();//获取canvas的元素信息
    query.exec(res => {

      var w = res[0].width;
      var h = res[0].height;
      var head_w = res[1].width;
      var head_h = res[1].height;

      wx.getImageInfo({
        src: pic,
        success: x => {
          ctx.drawImage(bgpic, 0, 0, w, h);
          ctx.drawImage(x.path, w / 1.55, h / 5.7, head_w, head_h);
          ctx.setFontSize(ft);
          ctx.font = "normal normal  Arial";
          
          ctx.fillStyle = '#222222';
          ctx.fillText(Date.parse(new Date()), w / 3.6, h / 5);

          ctx.fillText(name, w / 3.6, h / 3.92);

          ctx.fillText(classs, w / 3.6, h / 3.25);

          ctx.fillText(parseInt(100*Math.random()), w / 3.6, h / 2.73);

          ctx.fillText(parseInt(50 *Math.random()), w / 3.6, h / 2.35);

          ctx.fillText(school, w / 3.6, h / 2.08);
          
          
          ctx.draw(false, function () {
            
            app.buldingImg("mycanvas", callBack => {
              me.setData({
                shareImg: callBack.data.url,
                ajaxSuccess: true
              });
              wx.hideLoading();
            });
           
          });

        }
      })
    })

  },
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    console.log(this.data.shareImg)
    return {
      title: '准考证制作',
      desc: '准考证制作',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
      Success: function () {

      }
    }
  },
  onUnload() {
    wx.removeStorageSync("coverpic")
  },
  toPlay() {
    wx.navigateTo({
      url: `/pages/${pid}/${pid}_make/${pid}_make?pid=${pid}`,
    })
  },
  toHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
})